package com.copymd;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import lombok.SneakyThrows;
import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class md2queyu {

    String oldFilePath = "F:\\BaiduNetdiskDownload\\博学谷人工智能就业课（尊享无忧）\\00：人工智能极速就业班课程资料\\02阶段二相关资料";
    int count = 0;

    ExecutorService executor = Executors.newFixedThreadPool(10);

    @Test
    public void excuteCopyMd() {
        copyMD(oldFilePath);
    }

    @Test
    public void CMD() throws Exception {
        excute(oldFilePath);
    }

    @Test
    public void getHTMLnote() {
        copyeFile(oldFilePath);
    }

    public void copyMD(String path) {
        File dirs = new File(path);
        for (String fileStr : dirs.list()) {
            File file = new File(path + "\\" + fileStr);
            if (file.isDirectory()) {
                copyMD(path + "\\" + fileStr);
            } else {
                if (fileStr.endsWith("md")) {
                    String read = FileUtil.readString(path + "\\" + fileStr, "utf-8");
                    String replace = read.replace("  ```", "```");
                    FileUtil.writeString(replace, path + "\\" + fileStr, "utf-8");
                }
            }

        }
    }

    public void excute(String path) throws Exception {
        // 创建一个大小为5的线程池
        File dirs = new File(path);
        for (String fileStr : dirs.list()) {
            File file = new File(path + "\\" + fileStr);
            if (file.isDirectory()) {
                excute(path + "\\" + fileStr);
            } else {
                if (fileStr.endsWith("md")) {
                    String fullPath = file.getPath();
                    String regex = "[\u4e00-\u9fa5]+";
                    boolean matches = !fileStr.matches(".*" + regex + ".*");//判断文件名是否含有中文，含有中文可能是笔记，含有中文返回true，取反表示全英文
                    if (fullPath.contains("作业") || fullPath.contains("习题") || matches) {
                    } else {
                        Runtime runtime = Runtime.getRuntime();
                        Process exec = runtime.exec("mdtools.bat", null, new File(path));
                        System.out.println(path + "--执行完成");
                        Thread.sleep(1000);
                    }

                }
            }

        }
    }


    public void copyeFile(String path) {
        File dirs = new File(path);
        for (String fileStr : dirs.list()) {
            File file = new File(path + "\\" + fileStr);
            if (file.isDirectory()) {
                copyeFile(path + "\\" + fileStr);
            } else {
                if (fileStr.endsWith("html")) {
                    count++;
                    String oldFile = path + "\\" + fileStr;

                    String newFilePath = oldFile.replace("F:\\BaiduNetdiskDownload", "E:\\笔记归档目录");
                    File dirPath = new File(newFilePath).getParentFile();
                    if (dirPath.exists()) {
                        dirPath.mkdirs();
                    }

                    if (newFilePath.contains("作业") || newFilePath.contains("习题")) {

                    } else {
                        String oldFileTxt = FileUtil.readString(oldFile, "UTF-8");
                        String old = oldFileTxt.replace("!function(e){\"use strict\";function t(){\"complete\"===document.readyState?n():e.addEventListener(\"DOMContentLoaded\",n)}function n(){try{var e=document.querySelectorAll(\"code.hljs\");for(var t in e)e.hasOwnProperty(t)&&r(e[t])}catch(n){console.error(\"LineNumbers error: \",n)}}function r(e){if(\"object\"==typeof e){var t=e.parentNode,n=o(t.textContent);if(n>1){for(var r=\"\",c=0;n>c;c++)r+=c+1+\"\\n\";var l=document.createElement(\"code\");l.className=\"hljs hljs-line-numbers\",l.style[\"float\"]=\"left\",l.textContent=r,t.insertBefore(l,e)}}}function o(e){if(0===e.length)return 0;var t=/\\r\\n|\\r|\\n/g,n=e.match(t);return n=n?n.length:0,e[e.length-1].match(t)||(n+=1),n}\"undefined\"==typeof e.hljs?console.error(\"highlight.js not detected!\"):(e.hljs.initLineNumbersOnLoad=t,e.hljs.lineNumbersBlock=r)}(window);", "");
                        FileUtil.writeString(old, newFilePath, "UTF-8");
                    }
                }
            }

        }
    }

}
